package org.dromara.wmscloud.controller;

import java.util.List;

import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.wmscloud.common.IAsyncService;
import org.dromara.wmscloud.domain.bo.BaseMatGroupBo;
import org.dromara.wmscloud.domain.vo.BaseMatGroupVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.wmscloud.domain.vo.BaseUnitVo;
import org.dromara.wmscloud.domain.bo.BaseUnitBo;
import org.dromara.wmscloud.service.IBaseUnitService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 物料单位
 *
 * @author hou
 * @date 2025-04-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wmsCloud/unit")
public class BaseUnitController extends BaseController {

    private final IBaseUnitService baseUnitService;

    private final IAsyncService asyncService;

    /**
     * 查询物料组
     */
    @SaCheckPermission("base:unit:list")
    @GetMapping("/listAll")
    public R <List<BaseUnitVo>> listAllMatGroup() {
        BaseUnitBo bo = new BaseUnitBo();
        List<BaseUnitVo> list = baseUnitService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 查询物料单位列表
     */
    @SaCheckPermission("base:unit:list")
    @GetMapping("/list")
    public TableDataInfo<BaseUnitVo> list(BaseUnitBo bo, PageQuery pageQuery) {
        return baseUnitService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询物料主单位
     */
    @SaCheckPermission("base:unit:list")
    @GetMapping("/listprimary")
    public R <List<BaseUnitVo>> listPrimary() {
        BaseUnitBo bo = new BaseUnitBo();
        bo.setStatus("Y");
        bo.setPrimaryFlag("Y");
        return R.ok(baseUnitService.queryList(bo));
    }

    /**
     * 导出物料单位列表
     */
    @SaCheckPermission("base:unit:export")
    @Log(title = "物料单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseUnitBo bo, HttpServletResponse response) {
        List<BaseUnitVo> list = baseUnitService.queryList(bo);
        ExcelUtil.exportExcel(list, "物料单位", BaseUnitVo.class, response);
    }

    /**
     * 获取物料单位详细信息
     *
     * @param unitId 主键
     */
    @SaCheckPermission("base:unit:query")
    @GetMapping("/{unitId}")
    public R<BaseUnitVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long unitId) {
        return R.ok(baseUnitService.queryById(unitId));
    }

    /**
     * 新增物料单位
     */
    @SaCheckPermission("base:unit:add")
    @Log(title = "物料单位", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseUnitBo bo) {
        Boolean flag = baseUnitService.insertByBo(bo);
        //更新异步缓存
        if (flag) {
            this.asyncService.updateUnitToRedis();
        }
        return toAjax(flag);
    }

    /**
     * 修改物料单位
     */
    @SaCheckPermission("base:unit:edit")
    @Log(title = "物料单位", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseUnitBo bo) {
        Boolean flag = baseUnitService.updateByBo(bo);

        if (flag) {
            //更新异步缓存
            this.asyncService.updateUnitToRedis(Boolean.TRUE, List.of(bo.getUnitName()));
        }
        return toAjax(flag);
    }

    /**
     * 删除物料单位
     *
     * @param unitIds 主键串
     */
    @SaCheckPermission("base:unit:remove")
    @Log(title = "物料单位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{unitIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] unitIds) {
        return toAjax(baseUnitService.deleteWithValidByIds(List.of(unitIds), true));
    }
}
